package com.code9xs.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.code9xs.pojo.Activity;
import com.code9xs.pojo.Announcement;
import com.code9xs.pojo.Association;
import com.code9xs.pojo.User;
import com.code9xs.util.DateUtils;
import com.code9xs.util.IDPoolUtil;
import com.code9xs.util.PageUtil;
import com.code9xs.util.Result;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.security.RolesAllowed;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;

/**
 * @author Suhd
 * @date 2021/4/25
 * @time 14:21
 * @wechat i9xswanan
 * @github https://github.com/Code9xs
 */
@RestController
@RequestMapping("/association")
public class AssociationController extends BaseController{

    @PreAuthorize("hasAnyRole('ROLE_root_admin','ROLE_first_admin','ROLE_second_admin','ROLE_first_user','ROLE_second_user','ROLE_third_user','ROLE_fourth_user','ROLE_fifth_user')")
    @PostMapping("/application")
    public Result createAssociationApplication(@RequestBody JSONObject param){
        boolean flag = associationService.createAssociation(param);
        if (flag){
            return Result.succ(HttpServletResponse.SC_OK,"提交成功","");
        }
        return Result.fail(HttpServletResponse.SC_SERVICE_UNAVAILABLE,"社团名已存在","");
    }

    @GetMapping("/all_members")
    public Result getAllAssociationMember(@RequestParam("aid") String aid){
        return Result.succ(HttpServletResponse.SC_OK,"成功",associationService.getAllMembers(Integer.valueOf(aid)));
    }

    @PostMapping("/member")
    public Result modifyMemberInfo(@RequestBody JSONObject param){
        boolean flag = associationService.modifyMemberInfo(param);
        if (flag){
            return Result.succ(HttpServletResponse.SC_OK,"成功","");
        }
        return Result.fail(HttpServletResponse.SC_EXPECTATION_FAILED,"更新失败","");
    }

    @GetMapping("/information")
    public Result getAssociationInfo(@RequestParam("aid")String aid){
        return Result.succ(HttpServletResponse.SC_OK,"成功",associationService.getAssociationInfo(Integer.valueOf(aid)));
    }

    @PostMapping("/information")
    public Result modifyAssociationInfo(@RequestBody JSONObject param){
        associationService.modifyAssociationInfo(param);
        return Result.succ(HttpServletResponse.SC_OK,"成功","");
    }

    @GetMapping("/title")
    public Result getAssociationTitle(@RequestParam("aid") String aid){
        return Result.succ(HttpServletResponse.SC_OK,"成功",associationService.getAssociationTitle(Integer.valueOf(aid)));
    }

    @GetMapping("/department")
    public Result getAssociationDepartment(@RequestParam("aid") Integer aid){
        return Result.succ(HttpServletResponse.SC_OK,"成功",associationService.getAssociationDepartment(Integer.valueOf(aid)));
    }

    @GetMapping("/banner")
    public Result getBannerImg(){
        return Result.succ(HttpServletResponse.SC_OK,"成功",associationService.getAssociationActivityTenImg());
    }

    @PostMapping("/all")
    public Result getAllAssociationByPage(@RequestBody JSONObject param){
        return Result.succ(HttpServletResponse.SC_OK,"成功",associationService.getAllAssociation(param));
    }

    @GetMapping("/user_association")
    public Result getUserAssociation(){
        return Result.succ(HttpServletResponse.SC_OK,"成功",associationService.getUserAssociationInfo());
    }

    @GetMapping("/item")
    public Result getAssociationItemByAid(@RequestParam("aid") String aid){
        return Result.succ(HttpServletResponse.SC_OK,"成功",associationService.getAssociation(Integer.valueOf(aid)));
    }

    @GetMapping("/item/banner")
    public Result getItemBanner(@RequestParam("aid") String aid){
        return Result.succ(HttpServletResponse.SC_OK,"成功", associationService.getItemBanner(Integer.valueOf(aid)));
    }

    @GetMapping("/item/activity")
    public Result getAssociationActivity(@RequestParam String aid,@RequestParam String page,@RequestParam String size){
        return Result.succ(HttpServletResponse.SC_OK,"成功"
                ,associationService.getAssociationActivity(Integer.valueOf(aid),Integer.valueOf(page),Integer.valueOf(size)));
    }

    @GetMapping("/item/announce")
    public Result getAssociationAnnounce(@RequestParam("aid")String aid){
        return Result.succ(HttpServletResponse.SC_OK,"成功",associationService.getAssociationAnnounce(Integer.valueOf(aid)));
    }

    @PostMapping("/activity")
    public Result joinActivity(@RequestBody JSONObject param){
        User currentUser = userService.getCurrentUser();
        return associationService.joinActivity(currentUser.getId(),param.getInteger("aid"),param.getInteger("activityId"));
    }

    @PostMapping("/createActivity")
    public Result createActivity(@RequestBody JSONObject param){
        Activity activity = new Activity();
        Integer aid = param.getInteger("aid");
        String name = param.getString("name");
        String location = param.getString("location");
        Date startDate = param.getDate("startDate");
        String content = param.getString("content");
        String url = param.getString("url");
        Integer count = param.getInteger("count");
        activity.setId(IDPoolUtil.getOtherID());
        activity.setAid(aid);
        activity.setTime(startDate);
        activity.setName(name);
        activity.setContent(content);
        activity.setLocation(location);
        activity.setUrl(url);
        activity.setStatus("正在进行");
        activity.setCount(count);
        boolean flag = associationService.createActivity(activity);
        if (flag){
            return Result.succ(HttpServletResponse.SC_OK,"活动创建成功",null);
        }
        return Result.fail(HttpServletResponse.SC_FORBIDDEN,"活动创建失败",null);

    }

    @PostMapping("/launchAnnouncement")
    public Result createAnnouncement(@RequestBody JSONObject param){
        Announcement announcement = new Announcement();
        Integer aid = param.getInteger("aid");
        String subject = param.getString("subject");
        String content = param.getString("content");
        Integer validity = param.getInteger("validity");
        Integer uid = userService.getCurrentUser().getId();
        announcement.setAid(aid);
        announcement.setContent(content);
        announcement.setStatus("正在进行");
        announcement.setTime(DateUtils.getCurrentTime());
        announcement.setUid(uid);
        announcement.setSubject(subject);
        announcement.setValidityPeriod(validity);
        announcement.setDeletion(false);
        announcement.setType("社团公告");
        boolean flag = associationService.createAnnouncement(announcement);
        if (flag){
            return Result.succ(HttpServletResponse.SC_OK,"公告发布成功",null);
        }
        return Result.fail(HttpServletResponse.SC_FORBIDDEN,"公告发布失败",null);
    }

    @GetMapping("/user/activity")
    public Result getUserActivity(@RequestParam("currentPage") String currentPage,@RequestParam("pageSize")String pageSize){
        return associationService.getUserActivityByPagination(Integer.valueOf(currentPage),Integer.valueOf(pageSize));
    }

    @GetMapping("/association/people")
    public Result getAssociationPeopleByPagination(@RequestParam("aid")String aid,
                                                   @RequestParam("page")String page,
                                                   @RequestParam("pageSize")String pageSize){
        PageUtil data = associationService.getAssociationPeopleByPagination(Integer.valueOf(aid), Integer.valueOf(page), Integer.valueOf(pageSize));
        return Result.succ(HttpServletResponse.SC_OK,"成功",data);
    }

    // 该方法与/association/member 方法功能重复
    @PostMapping("/people/modify")
    public Result modifyPeopleInformation(@RequestBody JSONObject param){
        final boolean flag = associationService.modifyPeopleInformation(
                param.getInteger("uid"),
                param.getInteger("aid"),
                param.getInteger("titleId"),
                param.getInteger("departmentId"));
        if (flag){
            return Result.succ(HttpServletResponse.SC_OK,"更新成功",null);
        }
        return Result.fail(HttpServletResponse.SC_FORBIDDEN,"更新失败",null);
    }

    @PostMapping("/item/modifyActivity")
    public Result modifyActivity(@RequestBody JSONObject param){
        Activity activity = new Activity();
        Integer id = param.getInteger("id");
        Integer aid = param.getInteger("aid");
        String content = param.getString("content");
        Integer count = param.getInteger("count");
        String location = param.getString("location");
        String status = param.getString("status");
        Date time = param.getDate("time");
        String name = param.getString("name");
        activity.setCount(count);
        activity.setStatus(status);
        activity.setLocation(location);
        activity.setContent(content);
        activity.setAid(aid);
        activity.setId(id);
        activity.setTime(time);
        activity.setName(name);
        boolean flag = associationService.modifyActivity(activity);
        if (flag){
            return Result.succ(HttpServletResponse.SC_OK,"修改成功",null);
        }
        return Result.fail(HttpServletResponse.SC_FORBIDDEN,"修改失败",null);
    }

    @GetMapping("/item/announcement")
    public Result getAssociationAnnouncementPaginationByAid(
            @RequestParam("aid")String aid,
            @RequestParam("page") String page,
            @RequestParam("pageSize")String pageSize){
        PageUtil data = associationService.getAssociationAnnouncePaginationByAid(Integer.valueOf(aid), Integer.valueOf(page), Integer.valueOf(pageSize));
        return Result.succ(HttpServletResponse.SC_OK,"成功",data);
    }

    @GetMapping("/index/association/new")
    public Result getNewArticle(){

        return Result.succ(HttpServletResponse.SC_OK,"成功",associationService.getNewArticle());
    }


}
